package zombiedefense.map;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.geom.Point2D;
import java.util.Iterator;
import java.util.LinkedList;

/**
 * An object representing a path, or list of nodes.
 * Call getNextNode() to access the next node in the path.
 * If there are no more nodes remaining, the last node
 * will be returned.
 */
public class Path {

    private static Point2D.Double pathPoint = new Point2D.Double();
    private LinkedList<Node> list;
    private Iterator<Node> iterator;
    private Node current;

    public Path() {
        list = new LinkedList<Node>();
    }

    public void draw(Graphics2D g,int drawx,int drawy,TileMap map,Color c){	
	g.setColor(c);
	int x,y;
	for(Node n:list){
	    if(n != null){
		x = map.getScreenX(n.getX());
		y = map.getScreenY(n.getY());
		g.drawRect(x,y,map.getTileWidth(),map.getTileHeight());
		g.drawLine(x,y,x + map.getTileWidth(),y + map.getTileHeight());
		g.drawLine(x + map.getTileWidth(),y,x,y + map.getTileHeight());
	    }
	}
    }
    
    public void add(Node n) {
	//avoid concurrent modification
	if(iterator != null)return;
	
        list.addFirst(n);
    }

    private Node getNextNode() {
	if(iterator == null){
	    iterator = list.iterator();
	}
	
        if (iterator.hasNext()) {
            current = iterator.next();
            return current;
        } else {
            return current;
        }
    }

    /**
     * Returns a point object representing the center
     * of the next tile in the path. These values
     * should be copied quickly and the reference to
     * the point released, as the point object will
     * be used for other processes.
     * @return 
     */
    public Point2D.Double getNextCoords() {
        Node n = getNextNode();
        pathPoint.setLocation(n.getX() + .5,n.getY() + .5);
        return pathPoint;
    }
}